import pygame
import resources
import random
from constants import *
from events import *

class EnemySprite(pygame.sprite.Sprite):
	def __init__(self, eventManager, *groups):
		pygame.sprite.Sprite.__init__(self, groups)
		self.eventManager = eventManager
		self.health = 1
	#---------------------------------------------------------------------------
	
	def hit(damage=1):
		self.health -= damage
		if self.health < 0:
				self.kill()
	#---------------------------------------------------------------------------
	
	def kill(self):
		center = (self.rect.centerx, self.rect.centery)
		event = ExplosionEvent(ExplosionSprite(center))
		self.eventManager.post(event)
		pygame.sprite.Sprite.kill(self)
	#---------------------------------------------------------------------------
#-------------------------------------------------------------------------------
			

class Enemy1Sprite(EnemySprite):
	def __init__(self, eventManager, *groups):
		EnemySprite.__init__(self, eventManager, groups)
		self.image = resources.images['enemy1']
		self.rect = self.image.get_rect()
		self.rect.bottom = 0
		self.rect.left = random.randrange(1, WINDOW_HEIGHT - self.rect.width)
	#---------------------------------------------------------------------------
	
	def update(self):
		self.rect.bottom += 1
		if self.rect.top > WINDOW_HEIGHT:
			 self.remove(self.groups())

	#---------------------------------------------------------------------------
#-------------------------------------------------------------------------------

class Enemy2Sprite(EnemySprite):
	def __init__(self, eventManager, *groups):
		EnemySprite.__init__(self, eventManager, groups)
		self.image = resources.images['enemy2']
		self.rect = self.image.get_rect()
		self.rect.bottom = 0
		self.rect.left = random.randrange(1, WINDOW_HEIGHT - self.rect.width)
	#---------------------------------------------------------------------------
	
	def update(self):
		self.rect.bottom += 1
		if self.rect.top > WINDOW_HEIGHT:
			 self.remove(self.groups())
		if random.randrange(1, 25) == 1:
			shot = EnemyShoot1Sprite((self.rect.centerx, self.rect.bottom), random.randrange(-10, 11))
			self.eventManager.post(AddShotEvent(shot))

	#---------------------------------------------------------------------------
#-------------------------------------------------------------------------------

class Enemy3Sprite(EnemySprite):
	def __init__(self, eventManager, *groups):
		EnemySprite.__init__(self, eventManager, groups)
		self.image = resources.images['enemy3']
		self.rect = self.image.get_rect()
		self.rect.bottom = 0
		self.rect.left = random.randrange(1, WINDOW_HEIGHT - self.rect.width)
	#---------------------------------------------------------------------------
	
	def update(self):
		self.rect.bottom += 1
		if self.rect.top > WINDOW_HEIGHT:
			 self.remove(self.groups())
		if random.randrange(1, 15) == 1:
			shot = EnemyShoot1Sprite((self.rect.centerx, self.rect.bottom), 0)
			self.eventManager.post(AddShotEvent(shot))

	#---------------------------------------------------------------------------
#-------------------------------------------------------------------------------

class ExplosionSprite(pygame.sprite.Sprite):
	def __init__(self, center, *groups):
		pygame.sprite.Sprite.__init__(self, groups)
		self.frame = 0
		self.maxframe = 14
		self.image = resources.images['explosion'][self.frame]
		self.rect = self.image.get_rect()
		self.rect.centerx = center[0]
		self.rect.centery = center[1]
	#---------------------------------------------------------------------------

	def update(self):
		if self.frame < self.maxframe:
			self.image = resources.images['explosion'][int(self.frame)]
			self.frame += 0.5
		else:
			self.kill()
	#---------------------------------------------------------------------------
#-------------------------------------------------------------------------------


class ShipSprite(pygame.sprite.Sprite):
	def __init__(self, eventManager, *groups):
		self.eventManager = eventManager
		eventManager.registerListener(self)
		pygame.sprite.Sprite.__init__(self, groups)
		self.image = resources.images['ship']
		self.rect = resources.images['ship'].get_rect()
		self.rect.centerx = WINDOW_WIDTH / 2
		self.rect.bottom = WINDOW_HEIGHT - 10
	#---------------------------------------------------------------------------
	
	def kill(self):
		center = (self.rect.centerx, self.rect.centery)
		event = ExplosionEvent(ExplosionSprite(center))
		self.eventManager.post(event)
		pygame.sprite.Sprite.kill(self)
		self.eventManager.unregisterListener(self)
	#---------------------------------------------------------------------------

	def notify(self, event):
		if isinstance(event, InputEvent):
			self.rect.top += (event.input.down - event.input.up) * 4
			self.rect.left += (event.input.right - event.input.left) * 4
			if self.rect.top < 0: self.rect.top = 0
			if self.rect.bottom > WINDOW_HEIGHT-10: self.rect.bottom = WINDOW_HEIGHT-10
			if self.rect.left < 0: self.rect.left = 0
			if self.rect.right > WINDOW_WIDTH: self.rect.right = WINDOW_WIDTH

			if(event.input.shoot):
				shoot = ShootSprite()
				shoot.rect.centerx = self.rect.centerx
				shoot.rect.bottom = self.rect.top 
				event = ShootEvent(shoot)
				self.eventManager.post(event)
	#---------------------------------------------------------------------------
#-------------------------------------------------------------------------------

class ShootSprite(pygame.sprite.Sprite):
	def __init__(self, *groups):
		pygame.sprite.Sprite.__init__(self, groups)
		self.image = resources.images['shot1']
		self.rect = resources.images['shot1'].get_rect()
	
	#---------------------------------------------------------------------------
	
	def update(self):
		self.rect.top -= 8
		if(self.rect.bottom < 0):
			 self.kill()
#-------------------------------------------------------------------------------

class EnemyShoot1Sprite(pygame.sprite.Sprite):
	def __init__(self, position, vx, *groups):
		pygame.sprite.Sprite.__init__(self, groups)
		self.image = resources.images['shot2']
		self.rect = resources.images['shot1'].get_rect()
		self.rect.topleft = position
		self.vx = vx

	def update(self):
		self.rect.left += self.vx
		self.rect.bottom += 3

		if self.rect.bottom > WINDOW_HEIGHT:
			self.kill()

# class EnemyShoot2Sprite(pygame.sprite.Sprite):
# 	def __init__(self, vx, *groups):
# 		pygame.sprite.Sprite.__init__(self, groups)
# 		self.image = resources.images['shot2']
# 		self.rect = resources.images['shot1'].get_rect()

# 	def update():
# 		self.rect.left += vx